﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.BusinessRule.CallRoute;

public partial class Visit_List : PageBase
{
    private const string NEW_PAGE = "Customer_Select.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitForm();
        }
    }

    private void InitForm()
    {
        Jumpton.Persistent.ObjectTable ot;

        //经销商
        ddlChain.DataTextField = "DESCRIPTION";
        ddlChain.DataValueField = "CODE";
        ot = new ChainManager().GetCourseTable(this.MyStateManager.DomainID);
        if (ot != null)
        {
            ddlChain.DataSource = ot;//.FindByFilter("IsValid='true'");
            ddlChain.DataBind();
        }
        ddlChain.Items.Insert(0, new ListItem("-所有-", ""));

        DictionaryItemsManager dim = new DictionaryItemsManager();

        //区域层次
        ddlLevel.DataTextField = "Name";
        ddlLevel.DataValueField = "DictionaryItemId";
        ddlLevel.DataSource = dim.GetItemList(DictionaryEnum.ChannelType, this.MyStateManager.DomainID);
        ddlLevel.DataBind();
        ddlLevel.DataBind();
        ddlLevel.Items.Insert(0, new ListItem("--所有--", ""));

        //客户类别
        ddlClass.DataTextField = "Name";
        ddlClass.DataValueField = "DictionaryItemId";
        ddlClass.DataSource = dim.GetItemList(DictionaryEnum.CustomerClass, this.MyStateManager.DomainID);
        ddlClass.DataBind(); 
        ddlClass.Items.Insert(0, new ListItem("-所有-", ""));

        //销售渠道
        ddlChannel.DataTextField = "Name";
        ddlChannel.DataValueField = "DictionaryItemId";
        ddlChannel.DataSource = dim.GetItemList(DictionaryEnum.CustomerChannel, this.MyStateManager.DomainID);
        ddlChannel.DataBind();
        ddlChannel.Items.Insert(0, new ListItem("-所有-", ""));
    }

    private void BindGrid()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(" AND DOMAIN_ID='" + this.MyStateManager.DomainID + "' ");

        if (ddlChain.SelectedValue != string.Empty)
        {
            sb.Append(" And CHAIN_CODE='" + ddlChain.SelectedValue + "' ");
        }
        if (ddlChannel.SelectedValue != string.Empty)
        {
            sb.Append(" AND LOCAL_LEVEL1_CODE =  '" + ddlChannel.SelectedValue + "'  ");
        }
        if (ddlClass.SelectedValue != string.Empty)
        {
            sb.Append(" AND CUST_CLASS_CODE =  '" + ddlClass.SelectedValue + "'  ");
        }
        if (ddlLevel.SelectedValue != string.Empty)
        {
            sb.Append(" AND GEO_LEVEL_CODE ='" + ddlLevel.SelectedValue + "' ");
        }
        if (txtCode.Text != string.Empty)
        {
            sb.Append(" AND C.[CODE] LIKE '%" + txtCode.Text.Trim() + "%' ");
        }
        if (txtName.Text != string.Empty)
        {
            sb.Append(" AND C.[NAME] LIKE '%" + txtName.Text + "%' ");
        }
        DataSet ds = OrderHeaderManager.SearchCustomer(sb.ToString());
        if (ds != null && ds.Tables.Count > 0)
        {
            gvChannelType.DataSource = ds.Tables[0];
            gvChannelType.DataBind();
        }


    }

    protected void gvChannelType_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            base.Grid_RowDataBound(sender, e);
            ((RadioButton)e.Row.FindControl("RadioButton1")).Attributes.Add("onclick", "setRadio(this)");
        }
        catch (Exception)
        { }
    }

    protected void btnQueryCustomer_Click(object sender, EventArgs e)
    {
        BindGrid();
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        int selectCustomerNumber = 0;
        decimal customerID = 0;

        for (int i = 0; i < gvChannelType.Rows.Count; i++)
        {
            if (((RadioButton)gvChannelType.Rows[i].FindControl("RadioButton1")).Checked)
            {
                selectCustomerNumber = 1;
                customerID = Convert.ToDecimal(gvChannelType.DataKeys[i].Value);
                break;
            }
        }
        if (selectCustomerNumber == 0)
        {
            ShowMessage("请选择一个有效的客户");
            return;
        }
        CUSTOMERS customer = new CustomerManager().Load(customerID);
        if (customer != null)
        {
            lblCustomer.Text = customer.NAME;
        }
        else
        {
            lblCustomer.Text = "";
        }

        DataSet ds = RouteManager.SearchVisitHistory(customerID);
        gvVisit.DataSource = ds;
        gvVisit.DataBind();

        trResult.Visible = true;
        //trSearch.Visible = false;
    }


    protected void btnResearch_Click(object sender, EventArgs e)
    {



        trResult.Visible = false;
        //trSearch.Visible = true;
    }

    protected void gvVisit_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Grid_RowDataBound(sender,e);
            GridViewRow row = e.Row;
            decimal visitID = 0;
            string strSort = string.Empty;

            // Make sure we aren't in header/footer rows
            if (row.DataItem == null)
            {
                return;
            }

            //Find Child GridView control
            GridView gv = new GridView();
            gv = (GridView)row.FindControl("gvCPR");

            ClientScript.RegisterStartupScript(GetType(), "Expand", "<SCRIPT LANGUAGE='javascript'>expandcollapse('div" + ((DataRowView)e.Row.DataItem)["ID"].ToString() + "','one');</script>");

            decimal.TryParse(((DataRowView)e.Row.DataItem)["ID"].ToString(), out visitID);
            DataSet ds = RouteManager.GetVisitDetailByVisitID(visitID);
            if (ds != null && ds.Tables.Count > 0)
            {
                gv.DataSource = ds;
                gv.DataBind();
            }
        }
    }
}
